package com.cuit.blogsystem.service.Impl;

import com.cuit.blogsystem.DTO.CommentDTO;
import com.cuit.blogsystem.DTO.TextDTO;
import com.cuit.blogsystem.entity.Comment;
import com.cuit.blogsystem.entity.Text;
import com.cuit.blogsystem.entity.Type;
import com.cuit.blogsystem.entity.User;
import com.cuit.blogsystem.mapper.TextMapper;
import com.cuit.blogsystem.mapper.TypeMapper;
import com.cuit.blogsystem.mapper.UserMapper;
import com.cuit.blogsystem.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class userServiceImpl implements userService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TextMapper textMapper;
    @Autowired
    private TypeMapper typeMapper;
    /**
     * 用户登录
     *
     */
    @Override
    public User userLogin(User user) throws Exception {
        String username = user.getUsername();
        String password = user.getPassword();
        User loginUser = userMapper.getByUserName(username);
        if(!loginUser.getPassword().equals(password)){
            throw new Exception("用户名或密码错误");
        }
        return loginUser;
    }

    /**
     * 注册用户
     */
    @Override
    public void save(User user) {
        userMapper.add(user);
    }

    /**
     * 用户上传文章
     */
    @Override
    @Transactional
    public void upload(Text text) {
        LocalDateTime now = LocalDateTime.now();
        text.setCreateTime(now);
        String type = text.getType();
        Type ty = typeMapper.findByType(type);
        userMapper.addText(text);
        userMapper.setUserWithText(text);
        if(ty==null){
            typeMapper.addType(type);
        }
    }

    /**
     * 用户评论
     */
    @Override
    public void submit(CommentDTO commentDTO) {
        LocalDateTime now = LocalDateTime.now();
        Comment comment = new Comment();
        comment.setCreateTime(now);
        comment.setTextId(commentDTO.getId());
        comment.setContent(commentDTO.getContent());
        comment.setUserId(commentDTO.getUserId());
        comment.setUsername(commentDTO.getUsername());
        userMapper.addComment(comment);
    }

    /**
     * 用户删除文章
     */
    //使用事务注解
    //@Transactional
    @Override
    public void deleteText(Long textId) {
        userMapper.deleteText(textId);
        userMapper.deleteTextWithUser(textId);
        //删除文章下的评论
        textMapper.deleteComment(textId);
    }

    /**
     * 用户修改评论
     */
    @Override
    public void updateComment(CommentDTO commentDTO) {
        Comment comment = new Comment();
        LocalDateTime now = LocalDateTime.now();
        comment.setId(commentDTO.getId());
        comment.setCreateTime(now);
        comment.setContent(commentDTO.getContent());
        userMapper.updateComment(comment);

    }

    /**
     * 用户修改文章
     */
    @Override
    public void updateText(TextDTO textDTO) {
        Text text = new Text();
        text.setId(textDTO.getId());
        text.setTitle(textDTO.getTitle());
        text.setContent(textDTO.getContent());
        text.setType(textDTO.getType());
        userMapper.updateText(text);
    }

    /**
     * 用户删除评论
     */
    @Override
    public void deleteComment(Long id) {
        userMapper.deleteCommentById(id);
    }

    /**
     * 新增文章类型
     */
    @Override
    public void addType(Type type) {
        String typeName = type.getTypeName();
        typeMapper.addType(typeName);
    }

    @Override
    public User check(Long userId) {
        return userMapper.getMyself(userId);
    }

    @Override
    public void deleteType(String category) {
        userMapper.deleteType(category);
    }


}
